﻿<%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<%@ Import Namespace="Equipment.DataAccess" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Create New Asset
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="CurrentMenuIDContent" runat="server" >assetMenu</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>
        Create New Asset</h2><br />
    <% Using Html.BeginForm()%>
    <fieldset>
        <legend></legend>
        <%= Html.ValidationMessage("Status")%>
        <div id="formContainer">
        <table>
            <tr>
                <td width="130px">
                    <label for="BarcodeID">
                        Asset Barcode ID:</label>
                        <% If ViewData("AssetBarcodeRequired") IsNot Nothing Then%>
                            <span class="required-mark">*</span>
                        <%End If%>
                </td>
                <td>
                    <%= Html.TextBox("BarcodeID", ViewData("BarcodeID"), New With {.class = "text normal-width"})%>
                    <%= Html.ValidationMessage("BarcodeID")%>
                    <span id="barCodeErrMsg" class="error hidden"></span>
                </td>
            </tr>
            <tr>
                <td>
                    <label for="AssetName">
                        AssetName:</label>
                        <% If ViewData("AssetNameRequired") IsNot Nothing Then%>
                            <span class="required-mark">*</span>
                        <%End If%>
                </td>
                <td>
                    <%= Html.TextBox("AssetName", ViewData("AssetName"), New With {.class = "text normal-width"})%>
                    <%= Html.ValidationMessage("AssetName")%>
                </td>
            </tr>
            <tr>
                <td>
                    <label for="AssetType">
                        Asset Type:</label>
                        <% If ViewData("AssetTypeRequired") IsNot Nothing Then%>
                            <span class="required-mark">*</span>
                        <%End If%>
                </td>
                <td>
                    <%= Html.DropDownList("AssetType")%>
                    <%= Html.ValidationMessage("AssetType")%>
                </td>
            </tr>
            <tr>
                <td>
                    <label for="ModelNo">
                        Model #:</label>
                        <% If ViewData("ModelNoRequired") IsNot Nothing Then%>
                            <span class="required-mark">*</span>
                        <%End If%>
                </td>
                <td>
                    <%= Html.TextBox("ModelNo", ViewData("ModelNo"), New With {.class = "text normal-width"})%>
                    <%= Html.ValidationMessage("ModelNo")%>
                </td>
            </tr>
            <tr>
                <td>
                    <label for="SerialNo">
                        Serial #:</label>
                        <% If ViewData("SerialNoRequired") IsNot Nothing Then%>
                            <span class="required-mark">*</span>
                        <%End If%>
                </td>
                <td>
                    <%= Html.TextBox("SerialNo", ViewData("SerialNo"), New With {.class = "text normal-width"})%>
                    <%= Html.ValidationMessage("SerialNo")%>
                </td>
            </tr>
            <tr>
                <td>
                    <label for="Site">
                        Site:</label>
                </td>
                <td>
                    <%= Html.DropDownList("Site")%>
                </td>
            </tr>
            <tr>
                <td>
                    <label for="Department">
                        Department:</label>
                        <% If ViewData("DepartmentRequired") IsNot Nothing Then%>
                            <span class="required-mark">*</span>
                        <%End If%>
                </td>
                <td>
                    <div id="departmentContainer">
                        <%= Html.DropDownList("Department")%>
                        <%= Html.ValidationMessage("Department")%>
                    </div>
                </td>
            </tr>
            <% For Each item As AssetCustomField In ViewData("CustomTextFields")%>
            <tr>
                <td>
                      <label for="<%:String.Concat("textField_", item.ID) %>">
                        <%= Html.Encode(item.FieldName)%>:</label>
                        <% If item.Mandatory Then%>
                            <span class="required-mark">*</span>
                        <%End If%>
                </td>
                <td>
                    <%= Html.TextBox(String.Concat("textField_", item.ID), ViewData(String.Concat("textField_", item.ID)), New With {.class = "text normal-width"})%>
                    <%= Html.ValidationMessage(String.Concat("textField_", item.ID))%>
                </td>
            </tr>
            <% Next%>
            <% For Each item In ViewData("CustomDDFields")%>
            <tr>
                <td>
                      <label for="<%:String.Concat("ddField_", item.ID) %>">
                        <%= Html.Encode(item.FieldName)%>:</label>
                        <% If item.Mandatory Then%>
                            <span class="required-mark">*</span>
                        <%End If%>
                </td>
                <td>
                    <%= Html.DropDownList(String.Concat("ddField_", item.ID), New SelectList(item.Values.Split(";"), ViewData(String.Concat("ddField_", item.ID))))%>
                    <%= Html.ValidationMessage(String.Concat("ddField_", item.ID), "*")%>
                </td>
            </tr>
            <% Next%>
        </table>
        </div>
        <p>
            <input type="submit" class="button" value="Create" />
        </p>
    </fieldset>
    <% End Using%>
    <div>
        <%: Html.ActionLink("Back to List", "Index") %>
    </div>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#Site").change(function () {
                clearMessage();
                getDepartmentsBySite();
                checkBarCode();
            });

            $("#BarcodeID").focus(function () {
                clearMessage();
            });

            $("#BarcodeID").blur(function () {
                checkBarCode();
            });

        });

        function checkBarCode() {
            var locID = $("#Department").val();
            var barCode = $("#BarcodeID").val();
            jQuery.ajax({
                type: 'POST',
                data: { locID: locID, barCode: barCode },
                url: '<%= Html.AttributeEncode(Url.Action("CheckBarCode")) %>',
                dataType: "json",
                success: function (result) {
                    if (result.Result != "ok") {
                        $("#barCodeErrMsg").html(result.Message).fadeIn(1000);
                    }
                }
            });
        }

        function clearMessage() {
            $("#barCodeErrMsg").html("").fadeOut(500);
        }

        function getDepartmentsBySite() {
            showLoading("formContainer", true);
            var siteId = $("#Site").val();
            if (siteId === "") {
                return;
            }
            jQuery.ajax({
                type: 'POST',
                data: { id: siteId },
                url: '<%= Html.AttributeEncode(Url.Action("GetDepartmentsBySite")) %>',
                success: function (result) {
                    $('#departmentContainer').html(result);
                    hideLoading("formContainer");
                }
            });
        }

    </script>
</asp:Content>
